'use strict';

angular.module('core')

.factory('homeService', ['$q','$http', '$filter', function($q, $http, $filter) {
    return {
        getCompletePlanByDate: getCompletePlanByDate,
        getPieChartData: getPieChartData,
        getChartOption: getChartOption,
        getBarChartOption: getBarChartOption
        
    };

    //获取完工计划数据
    function getCompletePlanByDate (CompleteDate) { 
        return http({
            method: 'POST',
            url: '/api/CompletePlan/getCompletePlanByDate',
            data: {
                CompleteDate: CompleteDate      
            }
        });
    }

    //获取品质监控饼图数据
    function getPieChartData (ChartDate, WorkLocation) { 
        return http({
            method: 'POST',
            url: '/api/Home/queryBizRecordRate',
            data: {
                date: ChartDate,
                workLocation: WorkLocation      
            }
        });
    }

     function getBarChartOption(itemBadTypes,title){

        var seriesBar = [];
        var dataBar = [];
        var titleText = title+"缺陷分布";


        for (var i = 0; i < itemBadTypes.length; i++) {
            var temp = itemBadTypes[i];
            dataBar.push(temp.BadTypeName);
            seriesBar.push({
                value: temp.Num,
                name: temp.BadTypeName
            });
        };

        return {
            title : {
                text: titleText,
                x:'center'
            },
            tooltip : {
                trigger: 'item',
                formatter: "{b} : {c} ({d}%)"
            },
            // legend: {
            //     orient: 'vertical',
            //     left: 'left',
            //     data: dataBar
            // },
            series : [
                {
                    name: '缺陷类型',
                    type: 'pie',
                    radius : '60%',
                    center: ['50%', '50%'],
                    data: seriesBar,
                    itemStyle: {
                        normal: { 
                            label: { 
                                show: true, 
                                formatter: '{b}({d}%)' 
                            }, 
                            labelLine: {show:true}
                        },
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ] 
        };
    }


    function getChartOption (PieData) {

        var labelRight = {
            normal : {
                color: '#2ec7c9',
                label : {
                    show : true,
                    position : 'center'    
                },
                labelLine : {
                    show : false
                }
            }
        };
        var labelFromatter = {
            normal : {
                label : {
                    formatter: function (params){
                        return params.seriesName + '\n'
                        + (100 - params.percent).toFixed(2) + '%'
                    },
                    textStyle: {
                        baseline : 'bottom',
                        fontSize: '15',
                        fontWeight: 'bold',
                        color:'#2f4554'

                    }

                }
            }
        }

        // var radius = ['15%', '10%'];
        var radius = [40, 55];
        var seriesPie = [];
        var widthRate = 10;
        var heightRate = 20;
        //统计总体不良率图的数据
        var allPieData = [{value:PieData.all.AllQualifiedNum, name:'合格', itemStyle : labelRight}];
        for (var i = 0; i < PieData.itemBadTypes.length; i++) {
            allPieData.push({
                value:PieData.itemBadTypes[i].Num, name:PieData.itemBadTypes[i].BadTypeName
            });        
        }
        console.log(allPieData);
        seriesPie.push({
            name:'总体不良率',
            type:'pie',
            center : [widthRate+'%', heightRate+'%'],
            radius: radius,
            itemStyle : labelFromatter,
            
            label: {
                normal: {
                    show: false,
                }
            },
            labelLine: {
                normal: {
                    show: false
                }
            },
            // data:[
            //     {value:PieData.all.AllNum-PieData.all.AllQualifiedNum, name:'不合格', itemStyle : labelWrong},
            //     {value:PieData.all.AllQualifiedNum, name:'合格', itemStyle : labelRight}
            // ]
            data:allPieData
        });

        for (var i = 0; i < PieData.other.length; i++) {
            widthRate+=25;
            if(widthRate>100){
               widthRate = 10; 
               heightRate+=25;
            }
            var otherPieData = [{value:PieData.other[i].QualifiedNum, name:'合格', itemStyle : labelRight}];
            for (var j = 0; j < PieData.other[i].BadTypes.length; j++) {
                otherPieData.push({
                    value:PieData.other[i].BadTypes[j].Num, name:PieData.other[i].BadTypes[j].BadTypeName
                });        
            }
            seriesPie.push({
                name: PieData.other[i].ItemMasterCode,
                type:'pie',
                center : [widthRate+'%', heightRate+'%'],
                radius: radius,
                itemStyle : labelFromatter,
                
                label: {
                    normal: {
                        show: false,
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data:otherPieData
            });
        };

       

        return {
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            legend: {
                orient: 'horizontal',
                x: 'left',
                data:['合格']
            },
            series: seriesPie
            
        };
        
       
    }

    function http (cfg) { 
        var deferred = $q.defer();

        $http(cfg).success(function(result) {
            if (result.Data) {
                deferred.resolve(result.Data);
            } else {
                deferred.reject(result.Error);
            }
        }).error(function(err) {
            deferred.reject(err);
        });

        return deferred.promise;
    }
}]);